﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Caramel.Entities.Testing
{
	public class EntityListComparer<T> : IEqualityComparer<IEnumerable<T>> where T : IEntity
	{
		public EntityListComparer()
			: this(null)
		{
		}

		public EntityListComparer(IEqualityComparer<T> elementComparer)
		{
			this.ElementComparer = elementComparer;
		}

		public IEqualityComparer<T> ElementComparer { get; set; }

		public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
		{
			if (x.Count() != y.Count())
			{
				return false;
			}

			foreach (var entityX in x)
			{
				var entityY = x.Where(e => e.ID == entityX.ID).SingleOrDefault();

				if (entityY == null)
				{
					return false;
				}

				if ((this.ElementComparer != null) && !this.ElementComparer.Equals(entityX, entityY))
				{
					return false;
				}
				else if (!entityX.Equals(entityY))
				{
					return false;
				}
			}

			return true;
		}

		public int GetHashCode(IEnumerable<T> obj)
		{
			throw new NotImplementedException();
		}
	}
}
